using System;
using System.Data;
using System.Configuration;
using CodeWork.Library.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Data.SqlTypes;
using CodeWork.Library;
using System.Text;
using System.Data.SqlClient;

/// <summary>
/// ORM object that maps to Tbl_Link
/// </summary>
namespace CodeWork.DAL.BDHomePage
{
    public class Link : SqlServerDAO
    {
        /// <summary>
        /// Self table parameters
        /// </summary>
        private int _id;
		private string _linkUrl;
		private string _linkDescription;
		private double _currentRating;
		private int _totalRatingVote;
		private int _totalVisits;
		private int _linkCategoryId;
		private bool _flagged;

        /// <summary>
        /// Foriegn key parameters
        /// </summary>
        private List<LinkGroupAndLink> _linkLinkGroupAndLink;
		private bool _isLinkLinkGroupAndLinkLoaded;
		

        /// <summary>
        /// Self table properties
        /// </summary>
		public int Id
		{
			get { return this._id; }
			set { this._id = value; }
		}

		public string LinkUrl
		{
			get { return this._linkUrl; }
			set { this._linkUrl = value; }
		}

		public string LinkDescription
		{
			get { return this._linkDescription; }
			set { this._linkDescription = value; }
		}

		public double CurrentRating
		{
			get { return this._currentRating; }
			set { this._currentRating = value; }
		}

		public int TotalRatingVote
		{
			get { return this._totalRatingVote; }
			set { this._totalRatingVote = value; }
		}

		public int TotalVisits
		{
			get { return this._totalVisits; }
			set { this._totalVisits = value; }
		}

		public int LinkCategoryId
		{
			get { return this._linkCategoryId; }
			set { this._linkCategoryId = value; }
		}

		public bool Flagged
		{
			get { return this._flagged; }
			set { this._flagged = value; }
		}
		
        /// <summary>
        /// Foreign key properties, loaded on demand only
        /// </summary>
        public List<LinkGroupAndLink> LinkLinkGroupAndLink
		{
			get
			{
				if (!this._isLinkLinkGroupAndLinkLoaded)
				{
					List<Condition> conditions = new List<Condition>();
					conditions.Add(new Condition("link_id", ConditionOperator.Equal,this._id, JoinOperator.FirstValue));
					LinkGroupAndLink linkGroupAndLink = new LinkGroupAndLink();
					this._linkLinkGroupAndLink = linkGroupAndLink.Load(conditions);
					this._isLinkLinkGroupAndLinkLoaded = true;
				}
				return this._linkLinkGroupAndLink;
			}
			set
			{
				this._linkLinkGroupAndLink = value;
				this._isLinkLinkGroupAndLinkLoaded = true;
			}
		}
		

        /// <summary>
        /// Empty Constructor
        /// </summary>
        public Link() { }

        /// <summary>
        /// Parameterized constructor
        /// </summary>
        /// <param name="id"></param>
		/// <param name="linkUrl"></param>
		/// <param name="linkDescription"></param>
		/// <param name="currentRating"></param>
		/// <param name="totalRatingVote"></param>
		/// <param name="totalVisits"></param>
		/// <param name="linkCategoryId"></param>
		/// <param name="flagged"></param>
        public Link
        (
            int id,
			string linkUrl,
			string linkDescription,
			double currentRating,
			int totalRatingVote,
			int totalVisits,
			int linkCategoryId,
			bool flagged
        )
        {
            this._id = id;
			this._linkUrl = linkUrl;
			this._linkDescription = linkDescription;
			this._currentRating = currentRating;
			this._totalRatingVote = totalRatingVote;
			this._totalVisits = totalVisits;
			this._linkCategoryId = linkCategoryId;
			this._flagged = flagged;
        }

        /// <summary>
        /// Static save method. Saves the current instance in the table using the provided values using the
        /// primary key id. Expects that the Id is a primary key of an existing record in table.
        /// </summary>
        /// <param name="id"></param>
		/// <param name="linkUrl"></param>
		/// <param name="linkDescription"></param>
		/// <param name="currentRating"></param>
		/// <param name="totalRatingVote"></param>
		/// <param name="totalVisits"></param>
		/// <param name="linkCategoryId"></param>
		/// <param name="flagged"></param>
        /// <returns>Boolean result as success or failure</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool Save
        (
            int id,
			string linkUrl,
			string linkDescription,
			double currentRating,
			int totalRatingVote,
			int totalVisits,
			int linkCategoryId,
			bool flagged
        )
        {
            Link link = new Link();
            link.Id = id;
			link.LinkUrl = linkUrl;
			link.LinkDescription = linkDescription;
			link.CurrentRating = currentRating;
			link.TotalRatingVote = totalRatingVote;
			link.TotalVisits = totalVisits;
			link.LinkCategoryId = linkCategoryId;
			link.Flagged = flagged;

            return link.Save();
        }

        /// <summary>
        /// Saves the current instance and returns result
        /// </summary>
        /// <returns>Boolean result as success or failure</returns>
        public override bool Save()
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, Id);
			AddCommandParameter(command, "@LinkUrl", SqlDbType.NVarChar, 500, ParameterDirection.Input, LinkUrl);
			AddCommandParameter(command, "@LinkDescription", SqlDbType.NVarChar, 4000, ParameterDirection.Input, LinkDescription);
			AddCommandParameter(command, "@CurrentRating", SqlDbType.Decimal, 0, ParameterDirection.Input, CurrentRating);
			AddCommandParameter(command, "@TotalRatingVote", SqlDbType.Int, 0, ParameterDirection.Input, TotalRatingVote);
			AddCommandParameter(command, "@TotalVisits", SqlDbType.Int, 0, ParameterDirection.Input, TotalVisits);
			AddCommandParameter(command, "@LinkCategoryId", SqlDbType.Int, 0, ParameterDirection.Input, LinkCategoryId);
			AddCommandParameter(command, "@Flagged", SqlDbType.Bit, 0, ParameterDirection.Input, Flagged);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Link_Save.ToString());

            int returnValue = (int)GetOneData(command);
            return (returnValue == 0 ? false : true);
        }

        /// <summary>
        /// Saves the current instance based on provided conditions. This method assumes that
        /// conditions are provided in correct order and in proper format.
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>Boolean result as success or failure</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public bool Save(List<Condition> conditions)
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            PrepareConditionalParameters(command, conditions);

            AddCommandParameter(command, "@NewLinkUrl", SqlDbType.NVarChar, 500, ParameterDirection.Input, LinkUrl);
			AddCommandParameter(command, "@NewLinkDescription", SqlDbType.NVarChar, 4000, ParameterDirection.Input, LinkDescription);
			AddCommandParameter(command, "@NewCurrentRating", SqlDbType.Decimal, 0, ParameterDirection.Input, CurrentRating);
			AddCommandParameter(command, "@NewTotalRatingVote", SqlDbType.Int, 0, ParameterDirection.Input, TotalRatingVote);
			AddCommandParameter(command, "@NewTotalVisits", SqlDbType.Int, 0, ParameterDirection.Input, TotalVisits);
			AddCommandParameter(command, "@NewLinkCategoryId", SqlDbType.Int, 0, ParameterDirection.Input, LinkCategoryId);
			AddCommandParameter(command, "@NewFlagged", SqlDbType.Bit, 0, ParameterDirection.Input, Flagged);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Link_SaveConditional.ToString());

            int returnValue = (int)GetOneData(command);
            return (returnValue == 0 ? false : true);
        }

        /// <summary>
        /// Static creational method. This method actually adds a new entry in the table with
        /// the provided information.
        /// </summary>
        /// <param name="linkUrl"></param>
		/// <param name="linkDescription"></param>
		/// <param name="currentRating"></param>
		/// <param name="totalRatingVote"></param>
		/// <param name="totalVisits"></param>
		/// <param name="linkCategoryId"></param>
		/// <param name="flagged"></param>
        /// <returns>Instance of the created object</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static Link Create
        (
            string linkUrl,
			string linkDescription,
			double currentRating,
			int totalRatingVote,
			int totalVisits,
			int linkCategoryId,
			bool flagged
        )
        {
            Link link = new Link();
            link.LinkUrl = linkUrl;
			link.LinkDescription = linkDescription;
			link.CurrentRating = currentRating;
			link.TotalRatingVote = totalRatingVote;
			link.TotalVisits = totalVisits;
			link.LinkCategoryId = linkCategoryId;
			link.Flagged = flagged;

            return (link.Create() ? link : null);
        }

        /// <summary>
        /// Instance creational method. Inserts the current object as a new entry in table.
        /// </summary>
        /// <returns>Boolean result as success or failure</returns>
        public override bool Create()
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@LinkUrl", SqlDbType.NVarChar, 500, ParameterDirection.Input, LinkUrl);
			AddCommandParameter(command, "@LinkDescription", SqlDbType.NVarChar, 4000, ParameterDirection.Input, LinkDescription);
			AddCommandParameter(command, "@CurrentRating", SqlDbType.Decimal, 0, ParameterDirection.Input, CurrentRating);
			AddCommandParameter(command, "@TotalRatingVote", SqlDbType.Int, 0, ParameterDirection.Input, TotalRatingVote);
			AddCommandParameter(command, "@TotalVisits", SqlDbType.Int, 0, ParameterDirection.Input, TotalVisits);
			AddCommandParameter(command, "@LinkCategoryId", SqlDbType.Int, 0, ParameterDirection.Input, LinkCategoryId);
			AddCommandParameter(command, "@Flagged", SqlDbType.Bit, 0, ParameterDirection.Input, Flagged);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Link_Create.ToString());

            Id = (int)GetOneData(command);
            return (Id == 0 ? false : true);
        }

        /// <summary>
        /// Static deletion method. Deletes a record from table using the provided primary key id.
        /// </summary>
        /// <param name="id">Primary key</param>
        /// <returns>Boolean result as success or failure</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public static bool Delete(int id)
        {
            Link link = new Link();
            link.Id = id;

            return link.Delete();
        }

        /// <summary>
        /// Static deletion method. Deletes a list record from table using the provided primary keys id.
        /// </summary>
        /// <param name="ids">List of primarky key id</param>
        /// <returns>Boolean result as success or failure</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public static bool Delete(List<int> ids)
        {
            string csvIds = Utility.MakeCSV(ids);

            Link link = new Link();

            return link.Delete(csvIds);
        }

        /// <summary>
        /// Static deletion method. Deletes records from table using provided conditions.
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>Boolean result as success or failure</returns>
        public static bool Delete(List<Condition> conditions)
        {
            Link link = new Link();

            return link.ConditionalDelete(conditions);
        }

        /// <summary>
        /// This is helper method for conditional delete method above. Not for other general use.
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>Boolean result as success or failure</returns>
        private bool ConditionalDelete(List<Condition> conditions)
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            PrepareConditionalParameters(command, conditions);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Link_DeleteConditional.ToString());

            int returnValue = (int)GetOneData(command);
            return (returnValue == 0 ? false : true);
        }

        /// <summary>
        /// This is a helper method for multiple id delete method. Not for general use.
        /// </summary>
        /// <param name="csvIds">Comma seperated primary key values as string</param>
        /// <returns>Boolean result as success or failure</returns>
        private bool Delete(string csvIds)
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@Ids", SqlDbType.NVarChar, 500, ParameterDirection.Input, csvIds);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Link_DeleteMultiple.ToString());

            int returnValue = (int)GetOneData(command);
            return (returnValue == 0 ? false : true);
        }

        /// <summary>
        /// Instance deletion method. Deletes the current instance from table.
        /// </summary>
        /// <returns>Boolean result as success or failure</returns>
        public override bool Delete()
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, Id);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Link_DeleteOne.ToString());

            int returnValue = (int)GetOneData(command);
            return (returnValue == 0 ? false : true);
        }

        /// <summary>
        /// Static selection method. Used for loading a perticular data using primary key id.
        /// </summary>
        /// <param name="id">Primary key</param>
        /// <returns>Instance of the loaded object</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static Link Load(int id)
        {
            Link link = new Link();
            link.Id = id;

            if (link.Load())
                return link;
            else
                return null;
        }

        /// <summary>
        /// Instance selection method that fetches data using paging and sorting and also with 
        /// custom conditions.
        /// </summary>
        /// <param name="oderBy">Sorting order</param>
        /// <param name="startRow">Page start position</param>
        /// <param name="pageSize">Number of maximum record to be shown</param>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>List of instances of loaded objects</returns>
        public List<Link> Load(string orderBy, int startRow, int pageSize, List<Condition> conditions)
        {
            List<Link> links = new List<Link>();
			
			SqlCommand command = (SqlCommand)CreateCommand();
			
            PrepareConditionalParameters(command, conditions);

            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@OrderBy", SqlDbType.NVarChar, 50, ParameterDirection.Input, orderBy);
            AddCommandParameter(command, "@StartRow", SqlDbType.Int, 0, ParameterDirection.Input, startRow);
            AddCommandParameter(command, "@PageSize", SqlDbType.Int, 0, ParameterDirection.Input, pageSize);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Link_LoadConditionalPaged.ToString());

            List<BaseDAO> dataList = GetListData(command);

            foreach (BaseDAO data in dataList)
            {
                Link link = (Link)data;
                links.Add(link);
            }

            return links;
        }

        /// <summary>
        /// Counts the number of total record available
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>Count of the records</returns>
        public int LoadCount(List<Condition> conditions)
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            PrepareConditionalParameters(command, conditions);

            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Link_LoadConditionalPagedCount.ToString());

            int returnValue = (int)GetOneData(command);
            return returnValue;
        }

        /// <summary>
        /// Instance selection method that fetches data using paging and sorting.
        /// </summary>
        /// <param name="oderBy">Sorting order</param>
        /// <param name="startRow">Page start position</param>
        /// <param name="pageSize">Number of maximum record to be shown</param>
        /// <returns>List of instances of loaded objects</returns>
        public List<Link> Load(string orderBy, int startRow, int pageSize)
        {
            List<Link> links = new List<Link>();
            
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@OrderBy", SqlDbType.NVarChar, 50, ParameterDirection.Input, orderBy);
            AddCommandParameter(command, "@StartRow", SqlDbType.Int, 0, ParameterDirection.Input, startRow);
            AddCommandParameter(command, "@PageSize", SqlDbType.Int, 0, ParameterDirection.Input, pageSize);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Link_LoadPaged.ToString());

            List<BaseDAO> dataList = GetListData(command);

            foreach (BaseDAO data in dataList)
            {
                Link link = (Link)data;
                links.Add(link);
            }

            return links;
        }

        /// <summary>
        /// Counts the number of total record available
        /// </summary>
        /// <returns>Count of the records</returns>
        public int LoadCount()
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Link_LoadPagedCount.ToString());

            int returnValue = (int)GetOneData(command);
            return returnValue;
        }

        /// <summary>
        /// Instance selection method. Used for reloading the current object from table. This method uses
        /// the Id of the current object to load and expects that the Id is correct.
        /// </summary>
        /// <returns>Boolean result as success or failure</returns>
        public override bool Load()
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, Id);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Link_Load.ToString());

            List<BaseDAO> dataList = GetListData(command);

            foreach (BaseDAO data in dataList)
            {
                Link link = (Link)data;

                this._id = link._id;
				this._linkUrl = link._linkUrl;
				this._linkDescription = link._linkDescription;
				this._currentRating = link._currentRating;
				this._totalRatingVote = link._totalRatingVote;
				this._totalVisits = link._totalVisits;
				this._linkCategoryId = link._linkCategoryId;
				this._flagged = link._flagged;

                break;
            }

            return (dataList.Count > 0 ? true : false);
        }

        /// <summary>
        /// Instance selection method. Used for loading multiple record based on provided conditions.
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>List of instances of loaded objects</returns>
        public List<Link> Load(List<Condition> conditions)
        {
            List<Link> links = new List<Link>();

			SqlCommand command = (SqlCommand)CreateCommand();
			
            PrepareConditionalParameters(command, conditions);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Link_LoadConditional.ToString());

            List<BaseDAO> dataList = GetListData(command);

            foreach (BaseDAO data in dataList)
            {
                Link link = (Link)data;
                links.Add(link);
            }

            return links;
        }

        /// <summary>
        /// Prepares the conditional parameters for use with Create/ Save/ Load/ Delete methods
        /// that use custom condition. Prepares the user Command object for using conditions.
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        private void PrepareConditionalParameters(SqlCommand command, List<Condition> conditions)
        {
            StringBuilder condition = new StringBuilder();

            object id = null;
			object linkUrl = null;
			object linkDescription = null;
			object currentRating = null;
			object totalRatingVote = null;
			object totalVisits = null;
			object linkCategoryId = null;
			object flagged = null;

            for (int i = 0; i < conditions.Count; i++)
            {
                string fieldName = Utility.ToHungarianNotation(conditions[i].FieldName);

                if (conditions[i].JoiningCondition == JoinOperator.FirstValue)
                {
                    condition.Append("WHERE ");
                }
                else if (conditions[i].JoiningCondition == JoinOperator.And)
                {
                    condition.Append(" AND ");
                }
                else if (conditions[i].JoiningCondition == JoinOperator.Or)
                {
                    condition.Append(" OR ");
                }

                condition.Append(conditions[i].FieldName).Append(" ").Append(Condition.MapCondition(conditions[i].ConditionalOperator)).Append("@" + fieldName);

                if ((conditions[i].ConditionalOperator == ConditionOperator.Like ||
                    conditions[i].ConditionalOperator == ConditionOperator.NotLike))
                {
                    if (string.IsNullOrEmpty(conditions[i].Value.ToString()))
                        conditions[i].Value = "%%%";
                    else
                        conditions[i].Value = "%" + conditions[i].Value.ToString() + "%";
                }

                if (fieldName == TableColumns.Id.ToString())
                    id = conditions[i].Value;
                else if (fieldName == TableColumns.LinkUrl.ToString())
					linkUrl = conditions[i].Value;
				else if (fieldName == TableColumns.LinkDescription.ToString())
					linkDescription = conditions[i].Value;
				else if (fieldName == TableColumns.CurrentRating.ToString())
					currentRating = conditions[i].Value;
				else if (fieldName == TableColumns.TotalRatingVote.ToString())
					totalRatingVote = conditions[i].Value;
				else if (fieldName == TableColumns.TotalVisits.ToString())
					totalVisits = conditions[i].Value;
				else if (fieldName == TableColumns.LinkCategoryId.ToString())
					linkCategoryId = conditions[i].Value;
				else if (fieldName == TableColumns.Flagged.ToString())
					flagged = conditions[i].Value;
            }

            AddCommandParameter(command, "@Condition", SqlDbType.NVarChar, 2000, ParameterDirection.Input, condition.ToString());
            AddCommandParameter(command, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
			AddCommandParameter(command, "@LinkUrl", SqlDbType.NVarChar, 500, ParameterDirection.Input, linkUrl);
			AddCommandParameter(command, "@LinkDescription", SqlDbType.NVarChar, 4000, ParameterDirection.Input, linkDescription);
			AddCommandParameter(command, "@CurrentRating", SqlDbType.Decimal, 0, ParameterDirection.Input, currentRating);
			AddCommandParameter(command, "@TotalRatingVote", SqlDbType.Int, 0, ParameterDirection.Input, totalRatingVote);
			AddCommandParameter(command, "@TotalVisits", SqlDbType.Int, 0, ParameterDirection.Input, totalVisits);
			AddCommandParameter(command, "@LinkCategoryId", SqlDbType.Int, 0, ParameterDirection.Input, linkCategoryId);
			AddCommandParameter(command, "@Flagged", SqlDbType.Bit, 0, ParameterDirection.Input, flagged);
        }

        /// <summary>
        /// Binds the fetched data from data reader to Objects
        /// </summary>
        /// <param name="reader">Data reader supplied by BaseDAO</param>
        /// <returns>List of object instances that was bound</returns>
        protected override List<BaseDAO> BindData(DbDataReader reader)
        {
            List<BaseDAO> result = new List<BaseDAO>();

			if (reader == null)
                return null;

            while (reader.Read())
            {
                Link link = new Link
                (
					reader["id"] == System.DBNull.Value ? GetIdMinValue : (int)reader["id"],
                    reader["link_url"] == System.DBNull.Value ? string.Empty : (string)reader["link_url"],
					reader["link_description"] == System.DBNull.Value ? string.Empty : (string)reader["link_description"],
					reader["current_rating"] == System.DBNull.Value ? 0.00 : Convert.ToDouble(reader["current_rating"]),
					reader["total_rating_vote"] == System.DBNull.Value ? 0 : (int)reader["total_rating_vote"],
					reader["total_visits"] == System.DBNull.Value ? 0 : (int)reader["total_visits"],
					reader["link_category_id"] == System.DBNull.Value ? 0 : (int)reader["link_category_id"],
					reader["flagged"] == System.DBNull.Value ? false : (bool)reader["flagged"]
                );

                result.Add(link);
            }
            
            if(!reader.IsClosed)
                reader.Close();

            return result;
        }

        /// <summary>
        /// List of store procedures that this class use
        /// </summary>
        private enum StoreProcedureName
        {
            Sp_Link_Save = 0,
            Sp_Link_Create = 1,
            Sp_Link_DeleteOne = 2,
            Sp_Link_DeleteMultiple = 3,
            Sp_Link_DeleteConditional = 4,
            Sp_Link_SaveConditional = 5,
            Sp_Link_LoadConditional = 6,
            Sp_Link_Load = 7,
            Sp_Link_LoadConditionalPaged = 8,
            Sp_Link_LoadConditionalPagedCount = 9,
            Sp_Link_LoadPaged = 10,
            Sp_Link_LoadPagedCount = 11
        }

        /// <summary>
        /// List of table column names that this class use
        /// </summary>
        private enum TableColumns
        {
            Id = 0,
			LinkUrl = 1,
			LinkDescription = 2,
			CurrentRating = 3,
			TotalRatingVote = 4,
			TotalVisits = 5,
			LinkCategoryId = 6,
			Flagged = 7
        }
    }
}